import dayjs from 'dayjs';
import numeral from 'numeral';

export const formatKLineStrToKLineObj = (str: string) => {
  const arr = str.split(',');
  const [
    date,
    open,
    close,
    high,
    low,
    volume,
    amount,
    amplitude,
    chg,
    change,
    turn,
  ] = arr;

  return {
    date: dayjs(date).valueOf(),
    open,
    close,
    high,
    low,
    volume,
    amount,
    amplitude,
    chg, //涨跌幅
    change,
    turn,
  };
};

export const formatSimpleInfosToInfoObj = ({
  f2,
  f3,
  f4,
  f5,
  f6,
  f7,
  f8,
  f9,
  f10,
  f12,
  f13,
  f14,
  f15,
  f16,
  f17,
  f18,
  f20,
  f21,
  f23,
  f25,
  f115,
}:any) => {
  return {
    newest: f2, //最新价
    chg: f3, //涨跌幅
    change: f4, //涨跌额
    volume: f5, //成交量（手）
    amount: f6, //成交额
    amplitude: f7, //振幅
    turn: f8, //换手率
    PE: f9, //市盈率（动态）
    VR: f10, //量比
    code: f12, //股票代码
    marketCode: f13, //深市、沪市代码
    name: f14,
    high: f15,
    low: f16,
    open: f17,
    prevClose: f18, //昨日收盘价
    TMV: f20, //总市值
    CMV: f21, //流通市值
    PB: f23, //市净率
    TYChg: f25, //今年涨跌幅
    PETTM: f115, //市盈率（TTM）
  };
};

export const formatQtToKLineObj = ({
  f2,
  f3,
  f4,
  f5,
  f6,
  f7,
  f8,
  f12,
  f15,
  f16,
  f17,
}:any) => {
  return {
    open: f17,
    close: f2,
    high: f15,
    low: f16,
    volume: f5,
    amount: f6,
    amplitude: f7,
    chg: f3, //涨跌幅
    change: f4,
    turn: f8,
    code: f12,
  };
};

export const formatQtObjToKLineObj = ({
  newest: close, //最新价
  chg, //涨跌幅
  change, //涨跌额
  volume, //成交量（手）
  amount, //成交额
  amplitude, //振幅
  turn, //换手率
  code, //股票代码
  high,
  low,
  open,
}: any): any => ({
  open,
  close,
  high,
  low,
  volume,
  amount,
  amplitude,
  chg,
  change,
  turn,
  code,
});

export const formatTypeormFindPgination = ({ pageNo, pageSize }:any) => {
  const skip = (Math.max(1, pageNo) - 1) * pageSize;
  return {
    skip,
    take: pageSize,
  };
};

export const formatChineseAmount = (amount: string | number): string => {
  if (amount === '-') {
    return amount;
  }

  if (+amount >= 10000 && +amount < 100000000) {
    return `${numeral(+amount / 10000).format('0.00')}万`;
  } else if (+amount >= 100000000) {
    return `${numeral(+amount / 100000000).format('0.00')}亿`;
  } else {
    return numeral(+amount).format('0.00');
  }
};
